package com.caisejingyu.whales.home.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide

import com.caisejingyu.whales.home.R
import com.caisejingyu.whales.home.common.Defines.DEFALULT_PICTURE_POSITION
import com.caisejingyu.whales.home.data.FrameBo

class PhotoFrameAdapter(private val data: List<FrameBo>, val itemOnClickListener: ItemOnClickListener) : RecyclerView.Adapter<PhotoFrameAdapter.ViewHolder>() {
    var recordPosition = DEFALULT_PICTURE_POSITION
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_home_photo_frame, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (position == 0) {
            Glide.with(holder.itemView.context).load(R.mipmap.inoc_enable).into(holder.imageView)
        } else {
            Glide.with(holder.itemView.context).load(data[position].url).into(holder.imageView)
        }

        holder.itemView.isSelected = data[position].isSelect
        holder.itemView.setOnClickListener {
            data[recordPosition].isSelect = false
            data[position].isSelect = true
            itemOnClickListener.itemOnClick(position)
            recordPosition = position
            notifyDataSetChanged()
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var imageView: ImageView = view.findViewById(R.id.imageView)
    }


    interface ItemOnClickListener {
        fun itemOnClick(position: Int)
    }

}